package com.collect.controller;

import com.collect.common.Result;
import com.collect.pojo.CollectDTO;
import com.collect.service.CollectService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @Classname CollectController
 * @Description TODO
 * @Date 2025/3/10 09:39
 * @Created by xxx
 */
@Slf4j
@RestController
@RequestMapping("api/v1/collect_doc")
public class CollectController {

    @Autowired
    private CollectService collectService;

    @PostMapping(consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public Result collect_doc(@RequestParam("fileId") String fileId,
                              @RequestParam("fileName") String fileName,
                              @RequestParam("file") MultipartFile file,
                              @RequestParam("type") String type) {
        CollectDTO collectDTO = new CollectDTO(fileId, fileName, file, type);
        log.info("收到文件上传请求, fileId:{}, fileName:{}, type:{}", fileId, fileName, type);
        if (collectDTO.getFile() == null) {
            return Result.error(collectDTO.getFileId(), new String[]{"上传失败", "文件为空"});
        }
        try {
            collectService.collect_doc(collectDTO);
            return Result.success(collectDTO.getFileId());
        } catch (IOException e) {
            return Result.error(collectDTO.getFileId(), new String[]{"上传失败", e.getMessage()});
        }
    }
}
